<template>
 <div class="nav-header">
      <el-button @click="goBack" type="primary" size="large">
        <el-icon><ArrowLeft /></el-icon>返回
      </el-button>
    </div>
  <div class="house-list-container">
    <div class="house-item" v-for="item in data" :key="item.id" @click="goToDetail(item.id)">
      <div class="house-image">
        <img :src="item.images[0] || '/default-house.jpg'" alt="房屋图片">
        <span class="status-tag" v-if="item.status === 'available'">
          {{ [2, 3, 4].includes(item.type) ? '可售' : '可租' }}
        </span>
      </div>
      <div class="house-info">
        <h3 class="house-title">{{ item.title }}</h3>
        <div class="house-tags">
          <span class="tag">{{ item.house_type }}</span>
          <span class="tag">{{ item.decoration_type === 'medium' ? '精装' : '简装' }}</span>
          <span class="tag">{{ item.orientation === 'south' ? '朝南' : item.orientation }}</span>
          <span class="tag" v-for="(value, key) in item.facilities" :key="key" v-if="value">{{ getFacilityLabel(key) }}</span>
        </div>
        <div class="house-details">
          <span class="area">{{ item.area }}m²</span>
          <span class="floor">{{ item.floor }}/{{ item.total_floor }}层</span>
        </div>
        <div class="location-info">
          <el-icon><Location /></el-icon>
          <span>{{ item.district }} - {{ item.address }}</span>
        </div>
        <div class="price-info">
          <div class="price-section">
            <span class="price">{{ item.price }}</span>
            <span class="unit">{{ getPriceUnit(item.type) }}</span>
          </div>
          <div class="deposit-info" v-if="item.type === 1">押金：{{ item.deposit }}元</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { Location , ArrowLeft} from '@element-plus/icons-vue'

const router = useRouter()
const data = ref([])

const getFacilityLabel = (facility) => {
  const facilityLabels = {
    wifi: 'WiFi',
    heating: '暖气',
    washing_machine: '洗衣机',
    air_conditioning: '空调'
  }
  return facilityLabels[facility] || facility
}

const getPriceUnit = (type) => {
  switch (Number(type)) {
    case 1:
      return '元/月'
    case 2:
    case 3:
    case 4:
      return '元/m²'
    default:
      return '元/月'
  }
}
// 返回上一级
const goBack = () => {
  router.back()
}
const getdata = () => {
  const typeId = router.currentRoute.value.query.type || 'defaultType';
  axios.get(`http://127.0.0.1:8000/users/house/list/?type=${typeId}`).then(res => {
    data.value = res.data.results
  }).catch(error => {
    console.error('数据获取失败:', error);
  });
}

watch(() => router.currentRoute.value.query, (newQuery) => {
  if (newQuery.type) {
    getdata();
  }
}, { immediate: true });

// 跳转到详情页面
const goToDetail = (id) => {
  router.push({
    name: 'house-detail',
    query: { id }
  })
}
</script>

<style scoped>
.house-list-container {
  padding: 16px;
  background: #F5F5DC;
  min-height: 100vh;
}

.house-item {
  background: #FFFFFF;
  border-radius: 12px;
  margin-bottom: 16px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  padding: 16px;
  gap: 16px;
}

.house-image {
  position: relative;
  width: 160px;
  height: 120px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}

.house-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.status-tag {
  position: absolute;
  top: 8px;
  left: 8px;
  background: rgba(76, 175, 80, 0.9);
  color: white;
  padding: 4px 12px;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 500;
  letter-spacing: 1px;
}

.house-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.house-title {
  font-size: 18px;
  font-weight: 600;
  color: #000000;
  margin: 0;
  line-height: 1.4;
}

.house-tags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.tag {
  background: #F5F5F5;
  color: #000000;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.house-details {
  display: flex;
  gap: 16px;
  color: #000000;
  font-size: 14px;
  font-weight: 500;
}

.location-info {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666666;
  font-size: 14px;
}

.location-info .el-icon {
  font-size: 16px;
  color: #4CAF50;
}

.price-info {
  margin-top: auto;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.price-section {
  display: flex;
  align-items: baseline;
  background: #FFF5F5;
  padding: 4px 8px;
  border-radius: 4px;
}

.price {
  color: #FF5252;
  font-size: 24px;
  font-weight: 600;
}

.unit {
  color: #FF5252;
  font-size: 14px;
  margin-left: 4px;
  font-weight: 500;
}

.deposit-info {
  color: #666666;
  font-size: 14px;
}
</style>


